$('.addGoods').click(function () {
    $add = $(this)
    var goodsid = $add.attr('goodsid');
    // alert(1)
    // alert(goodsid)
    $.getJSON('/app/addcart/', {'goodsid': goodsid, 'means': 'add'}, function (data) {
        if (data['status'] == 204) {
            window.location.href = '/app/login/'
        }

        if (data['status'] == 201) {
            $add.prev().html('0')
        }
        else {
            $add.prev().html(data['c_goods_num'])

        }
        $("#total_price").html(data['total_price']);
        // alert(2)
    })
})

$('.decGoods').click(function () {
    $dec = $(this)
    // alert('123')
    var $li = $dec.parents('li');
    // alert($li)
    // var goodsid = $li.attr('id')

    var goodsid = $dec.attr('id');
    // alert(goodsid)
    $.getJSON('/app/addcart/', {'goodsid': goodsid, 'means': 'dec'}, function (data) {

        if (data['status'] == 204) {
            window.location.href = '/app/login/'
        }
        if (data['c_goods_num'] > -1) {
            $dec.next().html(data['c_goods_num']);

        }

        if (data['status'] == 201) {
            $li.remove()
        }
         $("#total_price").html(data['total_price']);
        // alert('456')

    })
})

$('.select').click(function () {
    cartid = $(this).attr('cartid')
    console.log(cartid)
    $span = $(this).find('span')
    // alert($span)
    $.getJSON('/app/chselect/', {'cartid': cartid}, function (data) {

            if (data['status'] == 201) {
                // alert('12')
                $span.html('');

            } else {
                $span.html('√');
            }
             if (data['is_all_select']){
                    $(".all_select span span").html("√");
                }else{
                    $(".all_select span span").html("");

                }
        $("#total_price").html(data['total_price']);
        })

})

 $(".all_select").click(function () {
        //全选的div
        var $all_select = $(this);
        //已选的列表
        var select_list = [];
        //没有选择列表
        var unselect_list = [];

        $(".confirm").each(function () {

            var $confirm = $(this);

            var cartid = $('.select').attr("cartid");
            // alert(cartid)


            if ($confirm.find("span").find("span").html().trim()) {
                select_list.push(cartid);
                // alert(select_list)
            } else {
                unselect_list.push(cartid);
                // alert(unselect_list)
            }
        })
        if (unselect_list.length > 0) {
            //传一个列表 是不可以的  可以传字符串   3#5#8
            $.getJSON("/app/allselect/", {"cart_list": unselect_list.join("#")}, function (data) {
                console.log(data);
                if (data['status'] === 200) {
                    $(".confirm").find("span").find("span").html('√');
                    $all_select.find("span").find("span").html("√");
                    $("#total_price").html(data['total_price']);
                }
            })
        } else {
            if (select_list.length > 0) {
                $.getJSON("/app/allselect/", {"cart_list": select_list.join("#")}, function (data) {
                    console.log(data);
                    if (data['status'] === 200) {
                        $(".confirm").find("span").find("span").html('');
                        $all_select.find("span").find("span").html('');
                        $("#total_price").html(data['total_price']);
                    }
                })
            }
            else {
                $(".all_select span span").html("");

            }
        }

    })

$(function () {
    $('#make_order').click(function () {
    // alert('456')
    window.location.href = ('/app/orderdetail/')
    // alert('123')
})


})
